<template>
    <div class="mod-role"  style="padding: 20px 10px;">
      <el-form :inline="true" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="pages.maskingName" placeholder="规则名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button  type="primary" @click="addOrUpdateHandle(1)">新增</el-button>
          <!-- <el-button type="danger" @click="deleteHandle(dataListSelections)" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="maskingName"
          header-align="center"
          align="center"
          label="规则名称">
        </el-table-column>
        <el-table-column
          prop="maskingTarget"
          header-align="center"
          align="center"
          label="规则替换值">
        </el-table-column>
        <el-table-column
          prop="maskingRegix"
          header-align="center"
          align="center"
          label="规则表达式">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
            <el-popconfirm title="确定删除吗？" icon-color="#f00" @confirm="deleteHandle([scope.row.id])">
              <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 10px;text-align: right;">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update  ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </template>

  <script>
      import request from '@/utils/request'
    import AddOrUpdate from './dataTmadd'
    export default {
      data () {
        return {
          dataList: [],
          pageIndex: 1,
          pageSize: 10,
          totalPage: 0,
          dataListLoading: false,
          dataListSelections: [],
          pages:{
            "maskingName": "",
            "page": 1,
            "limit": 10
          }
        }
      },
      components: {
        AddOrUpdate
      },
      created () {
        this.getDataList()
      },
      methods: {
        // 获取数据列表
        getDataList () {
          this.pages.page = this.pages.page+''
          this.pages.limit = this.pages.limit+''
          this.dataListLoading = true
          request({
            url: '/ht-datadevelop/decryption/list',
            method: 'post',
            data: this.pages
          }).then((res) => {
            if (res.code === 200) {
              this.dataList = res.data.list
              this.totalPage = res.data.totalCount
            } else {
              this.dataList = []
              this.totalPage = 0
            }
            this.dataListLoading = false
          })
        },
        // 每页数
        sizeChangeHandle (val) {
          this.pageSize = val
          this.pageIndex = 1
          this.getDataList()
        },
        // 当前页
        currentChangeHandle (val) {
          this.pageIndex = val
          this.getDataList()
        },
        // 多选
        selectionChangeHandle (val) {
          this.dataListSelections = []
          val.forEach((item,index)=>{
            this.dataListSelections.push(item.id)
          })
        },
        // 新增 / 修改
        addOrUpdateHandle (row) {
          row==1?this.$refs.addOrUpdate.show():this.$refs.addOrUpdate.editData(row)
        },
        // 删除
        deleteHandle (ids) {
          // this.$confirm(`确定删除?`, '提示', {
          //   confirmButtonText: '确定',
          //   cancelButtonText: '取消',
          //   type: 'warning'
          // }).then(() => {
            request({
              url:'/ht-datadevelop/decryption/deleteByIds',
              method: 'post',
              data: ids
            }).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          // }).catch(() => {})
        }
      }
    }
  </script>
